/*
 * @lc app=leetcode.cn id=876 lang=typescript
 *
 * [876] 链表的中间结点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function middleNode(head: ListNode | null): ListNode | null {
    if (!head) return null;
    let now = head;
    let count = 1;
    while (now.next) {
        now = now.next;
        count++;
    }
    let mid = Math.floor(count / 2);
    now = head;
    while (mid) {
        now = now.next!;
        mid--;
    }
    return now;
}
// @lc code=end
